<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水萤火科技有限公司
 * @Date: 2019-01-03 20:14:16
 * @Description: 公用的控制方法
 * @LastEditors: lzg2021 854378082@qq.com
 * @LastEditTime: 2022-09-02 10:23:52
 * @FilePath: \jichongchong-api\app\Http\Controllers\Console\Common\CommonController.php
 */

namespace App\Http\Controllers\Console\Common;

use App\Models\Rbac\Menu;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CommonController extends Controller {
	// 面包屑导航
	public function getBreadCrumbList(Request $req) {
		try {
			$label = $req->input('url');
			$label = substr($label,1);
			$self = Menu::where('url', $label)->select('id', 'name', 'url')->first();
            if(is_null($self)){
                $breadcrumb = [];
            }else{
                // SELECT t2.id,t2.name,t2.url FROM (SELECT @r AS _id, (SELECT @r := parent_id FROM ums_menu WHERE id = _id) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := 1, @l := 0) vars, ums_menu h WHERE @r != 0) t1 JOIN ums_menu t2 ON t1._id = t2.id ORDER BY t1.lvl DESC
                $breadcrumb = DB::select("SELECT t2.id,t2.name,t2.url,t2.parent_id FROM (SELECT @r AS _id, (SELECT @r := parent_id FROM ums_menu WHERE id = _id) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := ".$self->id.", @l := 0) vars, ums_menu h WHERE @r != 0) t1 JOIN ums_menu t2 ON t1._id = t2.id ORDER BY t1.lvl DESC");
            }
			return $this->resJson(200, '获取成功！', $breadcrumb);
		} catch (\Throwable $th) {
			return $this->resJson(500, '获取失败，请稍后再试！',$th->getMessage());
		}
	}
}
